package com.ruoyi.zhmk.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zhmk.domain.ZhmkAsk;
import com.ruoyi.zhmk.mapper.ZhmkCheckMapper;
import com.ruoyi.zhmk.domain.ZhmkCheck;
import com.ruoyi.zhmk.service.IZhmkCheckService;
import com.ruoyi.common.core.text.Convert;

/**
 * 人员请假审核Service业务层处理
 * 
 * @author sxl
 * @date 2021-06-15
 */
@Service
public class ZhmkCheckServiceImpl implements IZhmkCheckService 
{
    @Autowired
    private ZhmkCheckMapper zhmkCheckMapper;

    /**
     * 查询人员请假审核
     * 
     * @param checkId 人员请假审核ID
     * @return 人员请假审核
     */
    @Override
    public ZhmkCheck selectZhmkCheckById(Long checkId)
    {
        return zhmkCheckMapper.selectZhmkCheckById(checkId);
    }

    /**
     * 查询人员请假审核列表
     * 
     * @param zhmkCheck 人员请假审核
     * @return 人员请假审核
     */
    @Override
    public List<ZhmkCheck> selectZhmkCheckList(ZhmkCheck zhmkCheck)
    {
        return zhmkCheckMapper.selectZhmkCheckList(zhmkCheck);
    }

    /**
     * 新增人员请假审核
     * 
     * @param zhmkCheck 人员请假审核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZhmkCheck(ZhmkCheck zhmkCheck)
    {
        zhmkCheck.setCreateTime(DateUtils.getNowDate());
        int rows = zhmkCheckMapper.insertZhmkCheck(zhmkCheck);
        insertZhmkAsk(zhmkCheck);
        return rows;
    }

    /**
     * 修改人员请假审核
     * 
     * @param zhmkCheck 人员请假审核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZhmkCheck(ZhmkCheck zhmkCheck)
    {
        zhmkCheck.setUpdateTime(DateUtils.getNowDate());
        zhmkCheckMapper.deleteZhmkAskByAskId(zhmkCheck.getCheckId());
        insertZhmkAsk(zhmkCheck);
        return zhmkCheckMapper.updateZhmkCheck(zhmkCheck);
    }

    /**
     * 删除人员请假审核对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZhmkCheckByIds(String ids)
    {
        zhmkCheckMapper.deleteZhmkAskByAskIds(Convert.toStrArray(ids));
        return zhmkCheckMapper.deleteZhmkCheckByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人员请假审核信息
     * 
     * @param checkId 人员请假审核ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCheckById(Long checkId)
    {
        zhmkCheckMapper.deleteZhmkAskByAskId(checkId);
        return zhmkCheckMapper.deleteZhmkCheckById(checkId);
    }

    /**
     * 新增人员请假信息
     * 
     * @param zhmkCheck 人员请假审核对象
     */
    public void insertZhmkAsk(ZhmkCheck zhmkCheck)
    {
        List<ZhmkAsk> zhmkAskList = zhmkCheck.getZhmkAskList();
        Long checkId = zhmkCheck.getCheckId();
        if (StringUtils.isNotNull(zhmkAskList))
        {
            List<ZhmkAsk> list = new ArrayList<ZhmkAsk>();
            for (ZhmkAsk zhmkAsk : zhmkAskList)
            {
                zhmkAsk.setAskId(checkId);
                list.add(zhmkAsk);
            }
            if (list.size() > 0)
            {
                zhmkCheckMapper.batchZhmkAsk(list);
            }
        }
    }
}
